home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-16 | 4.0 KB | 198 lines | [TEXT/MPCC] |
- // Sound Object Handler for the WASTE Text Engine
- // by Michael F. Kamprath, kamprath@earthlink.net
- //
- // v1.0, 16 March 1995
- //
-
- #include <Icons.h>
- #include <SoundInput.h>
- #include "WASTE.h"
-
- #include "WE_snd_Handler.h"
- #include "WASTE_Objects.h"
-
- //
- // Sound Channel and current playing sound pointers
- //
- SndChannelPtr gSoundChannel = nil;
- SndListHandle gCurrentSound = nil;
-
- //
- // New Object Handler for sounds
- //
- pascal OSErr HandleNewSound(Point *defaultObjectSize,WEObjectReference objectRef)
- {
- (*defaultObjectSize).h = 32;
- (*defaultObjectSize).v = 32;
-
- return(noErr);
- }
- //
- // Dispose Object handler for sounds
- //
-
- pascal OSErr HandleDisposeSound(WEObjectReference objectRef )
- {
- Handle theSound;
-
- theSound = WEGetObjectDataHandle(objectRef);
-
- DisposHandle(theSound);
-
- return(MemError());
- }
- //
- // Draw Object handler for sounds
- //
- pascal OSErr HandleDrawSound(Rect *destRect, WEObjectReference objectRef )
- {
- OSErr iErr;
-
- iErr = PlotIconID(destRect,atNone,ttNone,kSoundIconID);
-
- return( iErr );
- }
- //
- // Click Object Handler for sounds
- //
- static long oldSoundClickTime = nil;
-
- pascal OSErr HandleClickSound( Point hitPt,
- short modifiers,
- long clickTime,
- WEObjectReference objectRef)
- {
- SndListHandle theSound = (SndListHandle)WEGetObjectDataHandle( objectRef );
- OSErr iErr = noErr;
-
-
- if (oldSoundClickTime + GetDblTime() > clickTime )
- {
- iErr = PlaySoundHandle( theSound );
- }
-
- oldSoundClickTime = clickTime;
-
- return( iErr );
- }
-
- //
- // PlaySelectedSound()
- // If a sound object, and only a sound object, is selected,
- // PlaySelectedSound() will play it.
- //
- OSErr PlaySelectedSound( WEHandle theWE )
- {
- WEObjectReference objectRef;
- SndListHandle theSound;
- OSErr iErr = noErr;
-
- iErr = WEGetSelectedObject( &objectRef, theWE );
- if (!iErr)
- {
- theSound = (SndListHandle)WEGetObjectDataHandle( objectRef );
- iErr = PlaySoundHandle( theSound );
- }
-
- return( iErr );
- }
- //
- // CreateNewSoundObject()
- // Uses built in sound recording to create a new sound object.
- //
- OSErr CreateNewSoundObject( WEHandle theWE )
- {
- OSErr iErr;
- SndListHandle sndHandle = nil;
- Point corner = {32,32};
-
- iErr = SndRecord(nil,corner,siGoodQuality,&sndHandle);
-
- if (iErr == noErr)
- {
- corner.h = 32;
- corner.v = 32;
- WEInsertObject( 'snd ', (Handle)sndHandle, corner, theWE );
- }
-
- return( noErr );
- }
-
- //
- // PlaySoundHandle()
- // Ultility routine to play a sound async. Maintains a sound channel
- // and the sound data to do so.
- //
-
- OSErr PlaySoundHandle( SndListHandle theSound )
- {
- OSErr iErr = noErr;
- SndCommand cmd;
-
- // check for sound channel
- if (!gSoundChannel)
- gSoundChannel = CreateNewSoundChannel();
-
- // If a sound is currently playing, stop it and dispose of it's buffer.
- if (gCurrentSound)
- {
- cmd.cmd = quietCmd;
- iErr = SndDoImmediate(gSoundChannel,&cmd);
- HUnlock( (Handle)gCurrentSound );
- DisposHandle( (Handle)gCurrentSound );
- gCurrentSound = nil;
- }
-
- // create a new buffer for the selected sound.
- gCurrentSound = theSound;
- iErr = HandToHand((Handle *)&(gCurrentSound));
-
- if (!iErr)
- {
- // Lock the buffer and play the sound.
- HLockHi( (Handle)gCurrentSound );
- iErr = SndPlay(gSoundChannel,gCurrentSound,true);
- }
-
- return(iErr);
- }
-
- //
- // CreateNewSoundChannel()
- // Used to create a new shound channel.
- //
- SndChannelPtr CreateNewSoundChannel( void )
- {
- OSErr iErr;
- SndChannelPtr chan;
-
- // allocate a sound channel
- chan = (SndChannelPtr)NewPtrClear(sizeof(SndChannel));
- if ( chan != nil )
- {
- chan->qLength = stdQLength; //128 sound commands
- iErr = SndNewChannel(&chan, sampledSynth, initMono, nil);
- }
- return(chan); // return SndChannelPtr
- }
-
- //
- // CheckSoundStatus()
- // Should be called periodically to dispose of unneeded sound buffers.
- //
- void CheckSoundStatus( void )
- {
- OSErr iErr;
- SCStatus theStatus;
-
- if (gSoundChannel)
- {
- iErr = SndChannelStatus(gSoundChannel,sizeof(SCStatus),&theStatus);
- if (( !theStatus.scChannelBusy )&&(gCurrentSound))
- {
- HUnlock( (Handle)gCurrentSound );
- DisposHandle( (Handle)gCurrentSound );
- gCurrentSound = nil;
- }
- }
- }